package com.zjdiepu.www.manage.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.LessInformationStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.base.IOtherAttachmentsService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.system.IBaseSalersmanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.YwExtHelper;

/**
 * 资料补登记Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月24日 14时43分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwSupplementBussinessController", produces="application/json;charset=UTF-8")
@Api(tags = {"资料补登记"}, produces="application/json;charset=UTF-8")
public class YwSupplementBussinessController extends BaseController {
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;
    @Resource
    private IBaseSalersmanService baseSalersmanService;
    @Resource
    private IDpBaseDealerInfoService dpBaseDealerInfoService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    @Resource
    private IOtherAttachmentsService otherAttachmentsService;
    @Resource
	private ICachedDictionaryService cachedDictionaryService;
    /**
     * @author xiongxiaotun <br/>
     * @Title: yuqiList <br/>
     * @Description: 资料补登记 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "资料补登记 ", response = Boolean.class, httpMethod="POST", tags={"资料补登记 "})
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "ywSupplementBussinessDetail", value = "业务主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "ywSupplementBussinessDetail", method = RequestMethod.POST)
    public ApiResponse ywSupplementBussinessDetail(String busiId) {
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	Map<String, Object> response = CollectionInitUtil.emptyMap();
    	YwBusiness ywBusiness = ywBusinessService.selectById(busiId);
    	ywBusinessVoMapper.mapper(ywBusiness);
    	response.put("ywBusiness", ywBusiness);
    	
    	if(ywBusiness != null && StringUtils.isNotBlank(ywBusiness.getCustomerNo())) {
	    	String history = ywBusinessService.queryHistoryByCustomerNo(ywBusiness.getCustomerNo());
	    	response.put("historyBusiness", StringUtils.isBlank(history)?"-":(history.startsWith(",")?history.substring(1):history));
    	}
    	if(ywBusiness != null) {
    		String salesmanId = ywBusiness.getSalesmanId();
    		if(StringUtils.isNotBlank(salesmanId)) {
    			BaseSalersman baseSalersman = baseSalersmanService.selectById(salesmanId);
    			if(baseSalersman != null) {
    				response.put("salesmanName", baseSalersman.getName());
    				if(StringUtils.isNotBlank(baseSalersman.getUnitId())) {
    					String unitId = baseSalersman.getUnitId();
    					BaseBusiUnit baseBusiUnit = baseBusiUnitService.selectById(unitId);
    					if(baseBusiUnit != null) {
    						String orgName = baseBusiUnit.getOrgName();
    						response.put("orgName", orgName);
    					}
    				}
    				
    			}
    		}
    		String dealerId = ywBusiness.getDealerId();
    		if(StringUtils.isNotBlank(dealerId)) {
    			DpBaseDealerInfo dpBaseDealerInfo = dpBaseDealerInfoService.selectById(dealerId);
    			if(dpBaseDealerInfo != null) {
    				response.put("dealerLevel", cachedDictionaryService.getName(dpBaseDealerInfo.getDealerLevel(), DataDicType.DEALER_LEVEL));
    				response.put("dealerName", dpBaseDealerInfo.getDealerName());
    			}
    		}
    	}
    	
    	//已有附件
//    	OtherAttachmentsExt otherAttachmentsExt = new OtherAttachmentsExt();
//    	otherAttachmentsExt.setBaseWhere();
//    	otherAttachmentsExt.withOutPage();
//    	otherAttachmentsExt.setDataid(busiId);
//    	otherAttachmentsExt.setIncludeFileLabels(ZI_LIAO_LABELS);
//    	List<OtherAttachments> otherAttachments = otherAttachmentsService.selectByExt(otherAttachmentsExt);
//    	String queryCreditApplyId = ywBusiness.getQueryCreditApplyId();
//    	otherAttachmentsExt.setDataid(queryCreditApplyId);
//    	otherAttachments.addAll(otherAttachmentsService.selectByExt(otherAttachmentsExt));
//    	List<String> labels = new ArrayList<String>();
//    	//查找没有上传的资料
//    	AttachmentLabel[] attachmentLabels = AttachmentLabel.values();
//    	out:for(AttachmentLabel attachmentLabel : attachmentLabels) {
//    		 String code = attachmentLabel.getCode();
//    		 if(!ZI_LIAO_LABELS.contains(code)) {
//    			 continue;
//    		 }
//    		 if(CollectionUtils.isNotEmpty(otherAttachments)) {
//    			for(OtherAttachments otherAttachment : otherAttachments){
//    				String fileLabel = otherAttachment.getFileLabel();
//    				if(code.equals(fileLabel)) {
//    					continue out;
//    				}
//    			}
//    		}
//    		labels.add(code);
//    	}
//    	response.put("labels", labels);
    	
    	return ApiResponse.success(response);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 欠资料业务 <br/>
     * @param ywBusinessExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "欠资料业务",response = YwBusiness.class, httpMethod="POST", tags={"资料补登记"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser sysUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(sysUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(sysUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
        ywBusinessQueryRequest.addLessInformationStatus(LessInformationStatus.QIAN_ZI_LIAO.getCode());
//        ywBusinessQueryRequest.setOrgId(currentUser.getOrgId());
//        ywBusinessQueryRequest.setOperatorId(currentUser.getId());
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
}
